package com.pantuo.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pantuo.common.exception.ServiceException;
import com.pantuo.common.utils.DataTablePage;
import com.pantuo.common.utils.TableRequest;
import com.pantuo.sys.dao.LineDao;
import com.pantuo.sys.pojo.Line;
import com.pantuo.sys.service.LineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LineServiceImpl implements LineService {


    @Autowired
    private LineDao lineDao;



    /**
     * 查询所有路线信息（可根据任何条件查询），路线中包含所属公司。
     * 并做分页处理
     *
     * @param request
     * @return
     */
    @Override
    public PageImpl<Object> findPageObjects(TableRequest request) {

        //获取起始索引
        int start = request.getStart();
        //获取页面大小
        int length = request.getLength();
        //获取查询条件，用map封装
        Map<String, String> filter = request.getFilter();
        //获取排序字段和排序规则 asc or desc
        List<Map<String, String>> orderList = request.getOrder();
        //一页数据的集合
        List<Map<String, Object>> commonRecords = null;
        //如果排序条件为空则默认按照创建时间排序(最后创建的用户排在最前面)
        if (orderList == null) {
            Long total = lineDao.getTotal(filter);
            if (total == 0) {
                throw new ServiceException("暂无记录");
            }
            commonRecords = lineDao.findAllObjects(filter, null, start, length);
            return new DataTablePage(new PageImpl<Map<String, Object>>(commonRecords, PageRequest.of(start, length), total));
        }
        //如果排序不为空
        Map<String, String> orderMap = orderList.get(0);
        Long total = lineDao.getTotal(filter);
        if (total == 0) {
            throw new ServiceException("暂无记录");
        }
        commonRecords = lineDao.findAllObjects(filter, orderList, start, length);
        return new DataTablePage(new PageImpl<Map<String, Object>>(commonRecords, PageRequest.of(start, length), total));
    }


    /**
     * 根据线路名称查出线路id
     *
     * @param lineName_s
     * @return
     */
    @Override
    public Integer findIdByName(String lineName_s) {
        QueryWrapper<Line> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("lineName_s", lineName_s);
        Integer id = lineDao.selectOne(queryWrapper).getId();
        return id;
    }

    @Override
    public List<Map<String, Object>> findAllNames(Integer id) {
        if (id == null) {
            throw new ServiceException("公司id不能为空");
        }
        return lineDao.findAllNames(id);
    }

    @Override
    public List<Map<String, Object>> findLineByCName(Integer id) {
        return lineDao.findLineByCName(id);
    }

    @Override
    public PageImpl<Object> lineRangeSelect(TableRequest request) {

        //获取起始索引
        int start = request.getStart();
        //获取页面大小
        int length = request.getLength();
        //获取查询条件，用map封装
        Map<String, String> filter = request.getFilter();
        //获取排序字段和排序规则 asc or desc
        List<Map<String, String>> orderList = request.getOrder();
        //一页数据的集合
        List<Map<String, Object>> commonRecords = null;
        //如果排序条件为空则默认按照创建时间排序(最后创建的用户排在最前面)
        String date_s = filter.get("date_s");
        String startDate_s = date_s.split("_To_")[0];
        String endDate_s = date_s.split("_To_")[1];
        if (orderList == null) {
            //如果sqlListSize为0则是单表查询获取总数，否则为多表查询获取总数
            Long total = lineDao.getTotal(filter);
            if (total == 0) {
                throw new ServiceException("暂无记录");
            }
            //如果sqlListSize为0则走单表获取数据，否则走多表查询获取数据
            commonRecords = lineDao.findAllObjects(filter, null, start, length);
            for (Map<String, Object> commonRecord : commonRecords) {
                commonRecord.put("date_s", startDate_s + "--" + endDate_s);
            }
            return new DataTablePage(new PageImpl<>(commonRecords, PageRequest.of(request.getPage(), length), total));
        }
        //如果排序不为空
        Map<String, String> orderMap = orderList.get(0);
        Long total = lineDao.getTotal(filter);
        //如果sqlListSize为0则是单表查询获取总数，否则为多表查询获取总数
        if (total == 0) {
            throw new ServiceException("暂无记录");
        }
        //如果sqlListSize为0则走单表获取数据，否则走多表查询获取数据
        commonRecords = lineDao.findAllObjects(filter, orderList, start, length);
        for (Map<String, Object> commonRecord : commonRecords) {
            commonRecord.put("date_s", startDate_s + "--" + endDate_s);
        }
        return new DataTablePage(new PageImpl<>(commonRecords, PageRequest.of(request.getPage(), length), total));
    }

    @Override
    public int findCountForLine() {
        String startDate = "2021-08-01";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String endDate = sdf.format(new Date());
        int lineStaCount = lineDao.findCountForLine(startDate, endDate);

        return lineStaCount;
    }




}
